
<template>
	<a-card :bordered="false">

		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="单据编号" name="billOrderNo">
						<a-input v-model:value="searchFormState.billOrderNo" placeholder="请输入单据编号" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="单据状态" name="billStatus">
						<a-select
							allow-clear
							v-model:value="searchFormState.billStatus"
							placeholder="请选择单据状态" :options="billStatusOptions"> </a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>

		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.billId"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('inWare')">
						<template #icon><plus-outlined /></template>
						采购入库
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'invoiceType'">
					{{ $TOOL.dictTypeData('fill_type', record.invoiceType) }}
				</template>
				<template v-if="column.dataIndex === 'billStatus'">
					{{ $TOOL.dictTypeData('bill_status', record.billStatus) }}
				</template>

				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="openDetail(record.billId)" >详情</a>
						<a @click="printBill(record.billId)" >打印</a>
						<span v-if="record.billStatus == 1">
							<a-popconfirm  title="确定要作废吗？可能会影响统计数据，请谨慎使用!" @confirm="deleteBill(record.billId)">
								<a-button  type="link" danger size="small">作废</a-button>
							</a-popconfirm>
						</span>
					</a-space>
				</template>

			</template>



		</s-table>

		<Form ref="formRef" @successful="tableRef.refresh(true)" />
		<a-modal ref="modalRef"
				 title="单据详情"
				 :mask-closable="false"
				 :destroy-on-close="true"
				 :draggable="true"
				 v-model:open="detailDialog"
				 :style="{width: '1200px',height: '1200px',}"
				 @ok="detailDialog = false">

			<a-divider class="divider-top"/>
			<a-row>
				<a-col :span="8">
					单据编号：{{detailData.header.billOrderNo}}
				</a-col>
				<a-col :span="8">
					原始单号：{{detailData.header.oldOrderNo}}
				</a-col>
				<a-col :span="8">
					单据日期：{{detailData.header.billDate}}
				</a-col>
			</a-row>
			<a-row class="detail-header-row">

				<a-col :span="8">
					供应商：{{detailData.header.supplier?.supplierName}}
				</a-col>
				<a-col :span="8">
					发票类型：{{detailData.header.invoiceType}}
				</a-col>

			</a-row>
			<a-row class="detail-header-row">
				<a-col :span="8">
					业务员：{{detailData.header.user?.name}}
				</a-col>
				<a-col :span="8">
					备注：{{detailData.header.orderRemark}}
				</a-col>
			</a-row>

			<div class="bill-detail-title">单据清单</div>
			<a-divider class="divider-center"/>
			<a-row>
				<a-table
					:dataSource="detailData.recordList"
					:columns="detailColumns"
					rowKey="id"
					:pagination="false"
					:scroll="{ x: 1500 }"
				></a-table>
			</a-row>

			<div style="padding-bottom: 50px"></div>
		</a-modal>

	</a-card>



</template>

<script setup name="billheader">
	import { cloneDeep } from 'lodash-es'
	import imsBillApi from '@/api/ims/imsBillApi'
	import Form from "./form.vue";
	import XnUserSelector from "@/components/XnUserSelector/index.vue";
	import sysConfig from "@/config";
	import tool from "@/utils/tool";


	const searchFormState = ref({})
	const tableRef = ref()
	const searchFormRef = ref(null)
	const formRef = ref()
	const billStatusOptions = ref([])
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '单据编号',
			dataIndex: 'billOrderNo'
		},
		{
			title: '原始单号',
			dataIndex: 'oldOrderNo'
		},
		{
			title: '单据状态',
			dataIndex: 'billStatus'
		},
		{
			title: '类型',
			dataIndex: 'billType'
		},
		{
			title: '单据日期',
			dataIndex: 'billDate'
		},
		{
			title: '总数量',
			dataIndex: 'orderNum'
		},
		{
			title: '总金额',
			dataIndex: 'orderAmount'
		},
		{
			title: '开单日期',
			dataIndex: 'createTime'
		},
		{
			title: '发票类型',
			dataIndex: 'invoiceType'
		},
		{
			title: '业务员',
			dataIndex: 'user.name',
			customRender: ({ record }) => record.user?.name
		},
		// {
		// 	title: '供应商',
		// 	dataIndex: 'supplier.supplierName',
		// 	customRender: ({ record }) => record.supplier.supplierName
		// },
		{
			title: '备注',
			dataIndex: 'orderRemark'
		},
	]
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		parameter.billType = '采购入库'
		return imsBillApi.imsBillPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteImsBill = (record) => {
		let params = [
			{
				billId: record.billId
			}
		]
		imsBillApi.imsBillDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchImsBill = (params) => {
		imsBillApi.imsBillDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}


	// 详情
	const detailDialog = ref(false)
	const detailData = ref({
		header: {
			supplier: {},
			warehouse: {},
			user: {}
		},
		recordList: [],
	})

	const detailColumns = [
		{
			title: '商品全名',
			dataIndex: 'productTotalName',
			key: 'productTotalName',
			width: 200,
			fixed: 'left',
		},
		{
			title: '商品规格',
			dataIndex: 'productSpecifications',
			key: 'productSpecifications',
			width: 120,
		},
		{
			title: '单位',
			dataIndex: 'measureUnit',
			key: 'measureUnit',
			width: 120
		},
		{
			title: '入库数量',
			dataIndex: 'planQuantity',
			key: 'planQuantity',
			width: 120
		},
		{
			title: '税率',
			dataIndex: 'texRate',
			key: 'texRate',
			width: 120
		},
		{
			title: '含税单价',
			dataIndex: 'univalence',
			key: 'univalence',
			width: 120
		},
		{

			title: '打折%',
			dataIndex: 'discount',
			key: 'discount',
			width: 120
		},
		{
			title: '含税金额',
			dataIndex: 'money',
			key: 'money',
			width: 120
		},
		{
			title: '税额',
			dataIndex: 'texMoney',
			key: 'texMoney',
			width: 120
		},
		{
			title: '备注',
			dataIndex: 'remark',
			key: 'remark',
			width: 120
		},
	];

	const openDetail = (id) => {
		detailDialog.value = true
		// 查询单据详情
		imsBillApi.imsBillDetail({billId: id}).then(res => {
			console.log('单据详情：', res)
			detailData.value = res
		})
	}
	const printBill = (id) => {
		window.open(`${sysConfig.API_URL}/jmreport/view/730940446969999360?token=${tool.data.get('TOKEN')}&id=${id}`, "_blank");
	}

	// 删除单据
	const deleteBill= (id) => {
		imsBillApi.deleteById({billId: id}).then(res => {
			console.log('获取删除结果：', res)
			tableRef.value.refresh(true)
		})
	}


	onMounted(() => {
		billStatusOptions.value = tool.dictList('bill_status')
	})
</script>
<style>
.detail-header-row {
	margin-top: 40px;
}

.bill-detail-title {
	margin-top: 40px;
	color: rgba(0, 0, 0, 0.88);
	font-weight: 600;
	font-size: 16px;
	line-height: 1.5;
	word-wrap: break-word;
}
</style>
